package com.code.wordhelper.utils;

import com.microsoft.schemas.office.office.CTLock;
import com.microsoft.schemas.vml.*;
import org.apache.poi.wp.usermodel.HeaderFooterType;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFHeader;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

public class WaterMarkUtils {
    private static final Integer widthPerWord = 10;

    public static void waterMark(XWPFDocument document, String text, String textColor, Integer fontSize, Integer textHeight, Integer styleTop, String rotation) {
        XWPFHeader header = document.createHeader(HeaderFooterType.DEFAULT);
        XWPFParagraph paragraph = header.getParagraphArray(0);
        if (paragraph == null) {
            paragraph = header.createParagraph();
        }

        //CTPicture没有单独添加shape的方法，所以只能全部添加所有元素
        CTP ctp = paragraph.getCTP();
        byte[] rsidr = document.getDocument().getBody().getPArray(0).getRsidR();
        byte[] rsidrdefault = document.getDocument().getBody().getPArray(0).getRsidRDefault();
        ctp.setRsidP(rsidr);
        ctp.setRsidRDefault(rsidrdefault);
        CTPPr ppr = ctp.addNewPPr();
        ppr.addNewPStyle().setVal("Header");
        // 开始加水印
        CTR ctr = ctp.addNewR();
        CTRPr ctrpr = ctr.addNewRPr();
        ctrpr.addNewNoProof();
        CTGroup group = CTGroup.Factory.newInstance();
        CTShapetype shapetype = group.addNewShapetype();
        CTTextPath shapeTypeTextPath = shapetype.addNewTextpath();
        shapeTypeTextPath.setOn(STTrueFalse.T);
        shapeTypeTextPath.setFitshape(STTrueFalse.T);
        CTLock lock = shapetype.addNewLock();
        lock.setExt(STExt.VIEW);
        CTShape shape = group.addNewShape();
        shape.setId("PowerPlusWaterMarkObject");
        shape.setSpid("_x0000_s102");
        shape.setType("#_x0000_t136");

        shape.setStyle(getShapeStyle(text, textHeight, styleTop, rotation));
        shape.setFillcolor(textColor);
        shape.setStroked(STTrueFalse.FALSE); // 字体设置为实心
        CTTextPath shapeTextPath = shape.addNewTextpath(); // 绘制文本的路径
        shapeTextPath.setStyle("font-family:10;font-size:" + fontSize); // 设置文本字体与大小
        shapeTextPath.setString(text);
        CTPicture pict = ctr.addNewPict();
        pict.set(group);
    }


    public static String getShapeStyle(String text, Integer textHeight, Integer styleTop, String rotation) {
        StringBuilder sb = new StringBuilder();
        sb.append("position: ").append("absolute");
        sb.append(";width: ").append(text.length() * widthPerWord).append("pt");
        sb.append(";height: ").append(textHeight + "pt");
        sb.append(";z-index: ").append("-251654144");
        sb.append(";mso-wrap-edited: ").append("f");
        sb.append(";margin-top: ").append(styleTop);
        sb.append(";mso-position-horizontal-relative: ").append("margin");
        sb.append(";mso-position-vertical-relative: ").append("margin");
        sb.append(";mso-position-vertical: ").append("left");
        sb.append(";mso-position-horizontal: ").append("center");
        sb.append(";rotation: ").append(rotation);
        return sb.toString();
    }
}
